package org.gjy.m8.date;

import java.time.LocalDate;
import java.time.MonthDay;
import java.time.temporal.ChronoUnit;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-10-13 16:25:48
 */
public class BirthdayUtil {

    public static int birthday(LocalDate birthday) {
        MonthDay now = MonthDay.now();
        MonthDay birth = MonthDay.from(birthday);

        return -1;
    }

    public static long daysUntilNextBirthday(LocalDate birthday) {
        if (birthday == null) {
            throw new IllegalArgumentException("birthday 不能为空");
        }

        LocalDate today = LocalDate.now();
        MonthDay birthMonthDay = MonthDay.from(birthday);   // 只保留 月‑日

        // ----- 处理闰年 2‑29 的特殊情况 -----
        if (birthMonthDay.equals(MonthDay.of(2, 29))) {
            // 当前年份是否闰年
            boolean thisYearLeap = today.isLeapYear();
            // 目标日期：今年的 2‑29（若是闰年）或 2‑28（非闰年，业务可自行改为 3‑01）
            LocalDate thisYearBirthday = thisYearLeap
                    ? LocalDate.of(today.getYear(), 2, 29)
                    : LocalDate.of(today.getYear(), 2, 28); // 业务约定：非闰年视为 2‑28

            if (!today.isAfter(thisYearBirthday)) { // 今天或之前
                return ChronoUnit.DAYS.between(today, thisYearBirthday);
            } else {
                // 已过今年生日，取明年的 2‑29（或 2‑28）
                int nextYear = today.getYear() + 1;
                boolean nextYearLeap = java.time.Year.isLeap(nextYear);
                LocalDate nextBirthday = nextYearLeap
                        ? LocalDate.of(nextYear, 2, 29)
                        : LocalDate.of(nextYear, 2, 28);
                return ChronoUnit.DAYS.between(today, nextBirthday);
            }
        }

        // ----- 普通生日（非 2‑29） -----
        LocalDate thisYearBirthday = birthMonthDay.atYear(today.getYear());

        if (!today.isAfter(thisYearBirthday)) {
            // 生日还没到（包括今天）
            return ChronoUnit.DAYS.between(today, thisYearBirthday);
        } else {
            // 已经过了今年生日，取明年同月同日
            LocalDate nextBirthday = birthMonthDay.atYear(today.getYear() + 1);
            return ChronoUnit.DAYS.between(today, nextBirthday);
        }

    }

}
